package outer;

import java.util.Arrays;

class Myshu
{
    public int[]arr;
    public Myshu()
    {
        this.arr=new int[10];
    }
    public int usesized;
    public  void add2(int a)
    {
        if(isfull())
        {
            this.arr= Arrays.copyOf(this.arr,2*arr.length);

        }
        this.arr[this.usesized]=a;
        this.usesized++;
    }


    public void add(int i,int a) throws Buhefa
    {
        if(isfull())
        {
            System.out.println("满了");
            arr= Arrays.copyOf(this.arr,2*this.arr.length);
        }
        else if(i>this.usesized||i<0)
        {
            System.out.println("i位置不合法");
            throw new Buhefa("此时i位置不合法");
        }
        int j=0;
        for(j=this.usesized-1;j>=i;j--)
        {
            this.arr[j+1]=this.arr[j];
        }

        this.arr[i]=a;
        this.usesized++;


    }
    public boolean contains (int a)
    {
        if(isEmpty())
        {
            System.out.println("数组为空");
            throw new EmptyException("此时数组为空");
        }
        int i=0;
        for(i=0;i<this.usesized;i++)
        {
            if(this.arr[i]==a)
            {
                return true;
            }
        }
        return false;
    }

    public int index(int tofind) //寻找某个元素的下标
    {
        if(isEmpty())
        {
            System.out.println("此时数组为空");
            throw new EmptyException("数组此时为空");
        }
        int i=0;
        for(i=0;i<this.usesized;i++)
        {
            if(this.arr[i]==tofind)
            {
                return i;
            }
        }
        return -1;

    }







    public void move(int c)    //移除第一个数字c
    {
        if(isEmpty())
        {
            //System.out.println("此时数组为空");
            throw new EmptyException("此时数组为空");
        }
        int d=this.index(c);
        if(d==-1)
        {
            System.out.println("不存在你要删除的数字");
        }
        for(int i=d;i<usesized-1;i++)
        {
            this.arr[d]=this.arr[d+1];

        }
        this.usesized--;

    }
    public void set(int i,int m)   //将数组中某个元素设置为某个值
    {
        if(i<0||i>=this.usesized)
        {
            System.out.println("此时i位置不合法");
            throw new Buhefa("此时i位置不合法");

        }
        else if(isEmpty())
        {
            throw new EmptyException("数组此时为空");

        }
        this.arr[i]=m;


    }
    public int search(int c)throws EmptyException    //寻找某个下标的元素
    {
        if(isEmpty())
        {
            throw new EmptyException("此时数组此时为空");

        }
        else if(c<0||c>=this.usesized)
        {
            System.out.println("此时i不合法");
        }
        return this.arr[c];
    }


    public void display()
    {
        int i=0;
        for(i=0;i<this.usesized;i++)
        {
            System.out.print(this.arr[i]+" ");
        }
        System.out.println();

    }
    public void clear()
    {
        this.usesized=0;
    }
    public boolean isEmpty()
    {
        return this.usesized==0;
    }

    public boolean isfull()
    {
        return this.usesized>=this.arr.length;

    }









}

public class text18 {
    public static void main(String[] args) {
        Myshu a=new Myshu();
        a.add(0,1);
        a.add(1,2);
        a.add(2,3);
        a.display();
        a.move(2);
        a.display();
        a.add2(3);
        a.display();
        a.add(1,9);
        a.display();
        System.out.println(a.contains(19));
        System.out.println(a.contains(9));
        try{
            a.add(-1,2);

        }catch(Buhefa e)
        {
            e.printStackTrace();

        }



    }








}
